<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="query" />
  <xsl:param name="pageindex" />
  <xsl:param name="frompage" />
  <xsl:param name="topage" />
  <xsl:param name="shownext" />
  <xsl:param name="showprev" />

  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <input name="k" value="{$query}" />
    <input type="submit" value="search"/>
    <br/>
    <xsl:choose>
      <xsl:when test="number(SearchResults/TotalResults) &gt; 0">
        Total results: <xsl:value-of select="SearchResults/TotalResults" />
        <br/>
        <br/>
        <xsl:if test="count(SearchResults/WebResults/ResultItem) &lt; SearchResults/TotalResults" >
          <xsl:call-template name="paging" />
          <br />
          <br />
        </xsl:if>
        <div class="bks_search_result">
          <ul>
            <xsl:apply-templates select="SearchResults/WebResults/ResultItem" />
          </ul>
        </div>
      </xsl:when>
      <xsl:otherwise>
        no search result for this query
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="paging">
    <xsl:if test="boolean($showprev)">
      <a href="Searchresults.aspx?k={$query}&amp;pageindex={$pageindex - 1}">previous</a>
      <xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text>
    </xsl:if>

    <xsl:call-template name="pagingrec">
      <xsl:with-param name="current" select="$frompage" />
    </xsl:call-template>

    <xsl:if test="boolean($shownext)">
      <a href="Searchresults.aspx?k={$query}&amp;pageindex={$pageindex + 1}">next</a>
    </xsl:if>
  </xsl:template>

  <xsl:template name="pagingrec">
    <xsl:param name="current" />
    <xsl:choose>
      <xsl:when test="$current != $pageindex">
        <a href="Searchresults.aspx?k={$query}&amp;pageindex={$current}">
          <xsl:value-of select="$current+1"/>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$current+1"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text>

    <xsl:if test="number($current) &lt; number($topage)">
      <xsl:call-template name="pagingrec">
        <xsl:with-param name="current" select="$current + 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="ResultItem">
    <li>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="Url" />
        </xsl:attribute>
        <strong>
          <xsl:value-of select="Title" />
        </strong>
      </a>
      <p>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="Description" />
          <xsl:with-param name="replace" select="'&lt;ddd/&gt;'" />
          <xsl:with-param name="with" select="'&#8230;'" />
        </xsl:call-template>
      </p>
    </li>
  </xsl:template>

  <xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)" disable-output-escaping="yes"/>
        <xsl:value-of select="$with" disable-output-escaping="yes"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" disable-output-escaping="yes"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


</xsl:stylesheet>
